<?php
namespace Foam\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\EventManager\EventManagerInterface;

class FicheTechniqueController extends AbstractActionController
{
    protected $ficheTable;
    protected $ingredientTable;
    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/foam');
        }, 100);
 
        return $this;
    }
    
    public function getFicheTechniqueTable()
    {
        if (!$this->ficheTable) {
            $sm = $this->getServiceLocator();
            $this->ficheTable = $sm->get('Foam\Model\FicheTechniqueTable');
        }
        return $this->ficheTable;
    }
    
    public function getIngredientTable()
    {
        if (!$this->ingredientTable) {
            $sm = $this->getServiceLocator();
            $this->ingredientTable = $sm->get('Foam\Model\IngredientTable');
        }
        return $this->ingredientTable;
    }   
    
    public function indexAction()
    {
        return new ViewModel();
    }
    
    public function ficheAction()
    {
        $id_produit = $this->params('id');
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        $viewModel->setVariable('fichetek', $this->getFicheTechniqueTable()->getFicheTechnique($id_produit));
        return $viewModel;
    }
    
    function addAction(){
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        $viewModel->setVariable('ingredients', $this->getIngredientTable()->fetchAll());
        return $viewModel;
    }
    
    function saveAction(){
        $request = $this->getRequest();
        if ($request->isPost()) {
            $id_fiche = ($request->getPost()->id_fiche)?$request->getPost()->id_fiche:'';
            $id_ingredient = ($request->getPost()->id_ingredient)?$request->getPost()->id_ingredient:0;
            $quantite = ($request->getPost()->quantite)?$request->getPost()->quantite:0;
            $condition = ($request->getPost()->condition)?$request->getPost()->condition:'';
            $conservation = ($request->getPost()->conservation)?$request->getPost()->conservation:'';
            $cadencier = ($request->getPost()->cadencier)?$request->getPost()->cadencier:'';
            $id_produit = ($request->getPost()->id_produit)?$request->getPost()->id_produit:0;
            
            $this->getFicheTechniqueTable()->saveFiche($id_fiche,$id_ingredient,$quantite,$condition,$conservation,$cadencier,$id_produit);
        }
        
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        return $viewModel;
    }
    
    public function cancelAction()
    {
        // Suppression des fiches avec un ID = 0
        $this->getFicheTechniqueTable()->deleteByCancel();
        
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        return $viewModel;
    }
}
?>
